
import { Controller, Post, Body, SetMetadata } from '@nestjs/common';
import { BaseDao } from '@/common/baseDao/base.service';
import { Util } from '@/common/util/util.service';
@SetMetadata('isLogger', true)
@Controller()
export class SystemRoleController {
    dbName: string;
    constructor(
        private baseDao: BaseDao,
        private util: Util,
    ) { 
        this.dbName = 'quick-roles'
    }


    @Post('/getList')
    async getList(@Body() data) {
        return await this.baseDao.getTableData({
            dbName: this.dbName,
            data,
            foreignDB:[
                {
                    dbName:'quick-permissions',
                    localKey:'permission',
                    foreignKey:'permission_id',
                    as:'permissionList'
                },
                {
                    dbName:'quick-menus',
                    localKey:'menu',
                    foreignKey:'menu_id',
                    as:'menuList'
                }
            ]
        })
        
    }

    @Post('/add')
    async add(@Body() data) {
        console.log('data',data)
        return await this.baseDao.add({
            dbName: this.dbName,
            dataJson: data
        })
    }

    @Post('/update')
    update(@Body() data) {
        console.log('data', data)
        const {
            _id,
            role_id,
            role_name,
            comment
        } = data;
        return this.baseDao.updateById({
            id: _id,
            dbName: this.dbName,
            dataJson: {
                role_id,
                role_name,
                comment
            }
        })
    }

    @Post('/delete')
    async delete(@Body() data) {
        console.log('data', data)
        return await this.baseDao.del({
            dbName: this.dbName,
            whereJson: data
        })
    }

    @Post('/bindMenu')
    async bindMenu(@Body() data) {
        const {
            _id,
            menu
        }= data;
        return await this.baseDao.updateById({
            id:_id,
            dbName: this.dbName,
            dataJson: {
                menu: menu
            }
        })
    }
    
}